You can register the task inside the init function of the App struct.
@main
struct MyApp: App {
init(){
// Your code
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Or, to use AppDelegate in a SwiftUI lifecycle app, first create a custom class of App Delegate.
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
return true
}
}
Then use UIApplicationDelegateAdaptor property wrapper to tell SwiftUI to use your AppDelegate class.
@main
struct MyApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}